const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';

class MyPromise {
    #status = PENDING;
    #value = undefined;
    #reason = undefined;
    #successCallBack = undefined;
    #errorCallBack = undefined;

    constructor(exec) {
        exec(this.resolve, this.reject);
    }

    resolve = (res) => {
        if (this.#status === PENDING) {
            this.#status = FULFILLED;
            this.#value = res;
            typeof this.#successCallBack === 'function' && this.#successCallBack(res);
        }
    }

    reject = (res) => {
        if (this.#status === PENDING) {
            this.#status = REJECTED;
            this.#reason = res;
            typeof this.#errorCallBack === 'function' && this.#errorCallBack(res);
        }
    }

    then = (onSuccess, onError) => {
        if (this.#status === FULFILLED) {
            onSuccess(this.#value);
        } else if (this.#status === REJECTED) {
            onError(this.#reason);
        } else {
            this.#successCallBack = onSuccess;
            this.#errorCallBack = onError;
        }
    }
}

let p = new MyPromise((resolve, reject) => {
    setTimeout(() => {
        resolve('OK');
    }, 1000);
});

p.then((res) => {
    console.log("%c Line:50 🍒 res", "color:#3f7cff", res);

}, (error) => {
    console.log("%c Line:53 🥤 error", "color:#ea7e5c", error);

})
